<?php
use Zend\Json\Json;

$groupCode = $this->groupCode;
$categoryAsKendoDataSourceArr = $this->categoryAsKendoDataSourceArr;
$categoryAsKendoDataSourceJson = Json::encode($categoryAsKendoDataSourceArr);
$rootCategory = $this->rootCategory;

/*$category = new Category();
$categoryAsKendoDataSourceArr = $category->getCategoryAsKendoDataSource($groupCode);
$categoryAsKendoDataSourceJson = Zend_Json::encode($categoryAsKendoDataSourceArr);

$rootCategory = $category->getRootCategoryOfGroup($groupCode, Core_Tool_Session::getCurrentLang());*/
?>

<script type="text/javascript">

    var categoryAsKendoDataSourceJson = <?php echo $categoryAsKendoDataSourceJson?>;
    if(treeview)
    treeview.destroy();
    var treeview = $("#category_tree_ctn_<?php echo $rootCategory->group_code ?>").kendoTreeView({
        dataSource: categoryAsKendoDataSourceJson,
        change: onSelectCategory
    }).data("kendoTreeView");


    treeview.expand(".k-item");
    treeview.select(".k-first");
    function onSelectCategory(e){
        selectedNode = getTreeSelectedNode(treeview)
        data = treeview.dataItem(selectedNode);

        if(data.modifiable == 0){
            $(".category_ctrl_ctn").find('a').each(function(){
                $(this).hide()
            })
        }else{
            $(".category_ctrl_ctn").find('a').each(function(){
                $(this).show()
            })
        }
        $('#btnAddCategory_<?php echo $rootCategory->id ?>').show();
        $.post('<?php echo '/admin/catalog/get-category-parent'  ?>',
                {id:data.id}
                ,function(result){
                    if (result.success){
                        if(result.upable)
                            $('#btnUpCategory_<?php echo $rootCategory->id ?>').show();
                        else
                            $('#btnUpCategory_<?php echo $rootCategory->id ?>').hide();
                        if(result.downable)
                            $('#btnDownCategory_<?php echo $rootCategory->id ?>').show();
                        else
                            $('#btnDownCategory_<?php echo $rootCategory->id ?>').hide();
                    }else{
                        $('#error_ctn').removeClass('blue_text').html(result.msg);
                        alert(result.msg);
                    }
                },'json');


    }

    function getTreeSelectedNode(treeview){
        selectedNode = treeview.select();
        if (selectedNode.length == 0) {
            treeview.select(".k-item:first");
            selectedNode = treeview.select();
        }
        return selectedNode;
    }

</script>